﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;


public partial class UserPlan_UserPlanManager : System.Web.UI.Page
{
    UserPlanMonth dllUserPlanMonth = new UserPlanMonth();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    Authentication dllAuthentication = new Authentication();

    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        if (!IsPostBack)
        {

            #region 弹出提醒
            PopupManager dllPopupManager = new PopupManager();
            string _info = dllPopupManager.getPopupInfo();
            if (_info.Length > 0)
            {
                ShowMessage(_info);
            }
            #endregion
        }

    }

    /// <summary>
    /// 弹出小窗体事件
    /// </summary>
    /// <param name="msg"></param>
    public void ShowMessage(string msg)
    {
        EeekSoft.Web.PopupWin pop = new EeekSoft.Web.PopupWin();
        this.Controls.Add(pop);
        pop.Text = msg;
        pop.Message = msg;
        pop.Title = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        pop.WindowScroll = true;
        pop.HideAfter = 99999999;
        pop.DockMode = EeekSoft.Web.PopupDocking.BottomLeft;
        pop.OffsetX = int.Parse(Session["_width"].ToString()) - 338;
        pop.ActionType = EeekSoft.Web.PopupAction.MessageWindow;
        pop.OffsetY = 1;
        pop.Width = 220;
        pop.Height = 170;
    }


    /// <summary>
    /// 绑定显示提交审核的文件的gridview事件
    /// </summary>
    void bindTiJiaoGridView()
    {
        ListItem _type = this.drpType.SelectedItem;
        ListItem _state = this.drpState.SelectedItem;
        DataTable _UserPlan = new DataTable();
        if (_type != null && _state != null)
        {
            //月提交
            if (_state.Value == "1"&&_type.Value=="0")
            {
                //删除按钮隐藏事件
                this.GvUserPlanList.Columns[6].Visible = false;
                object[] _value = new object[3];
                _value[0] = _type.Value;
                _value[1] = Convert.ToInt32(_state.Value);
                _value[2] = 0;
                _UserPlan = dllUserPlanMonth.getPlanSendStateTrue(_value);
                this.GvUserPlanList.DataSource = _UserPlan;
                this.GvUserPlanList.DataBind();                
            }
            //周提交
            if (_state.Value == "1" && _type.Value == "1")
            {
                //删除按钮隐藏事件
                this.GvUserPlanList.Columns[6].Visible = false;
                object[] _value = new object[3];
                _value[0] = _type.Value;
                _value[1] = Convert.ToInt32(_state.Value);
                _value[2] = 0;
                _UserPlan = dllUserPlanMonth.getPlanSendStateTrue(_value);
                this.GvUserPlanList.DataSource = _UserPlan;
                this.GvUserPlanList.DataBind();
            }
        }

        #region 处理显示
        //判断计划类型，把int类型的改写成字符类型显示出来
        for (int i = 0; i < this.GvUserPlanList.Rows.Count; i++)
        {
            if (_UserPlan.Rows[i]["PlanType"].ToString() == "0")
            {
                this.GvUserPlanList.Rows[i].Cells[1].Text = "月计划";
            }
            else
            {
                this.GvUserPlanList.Rows[i].Cells[1].Text = "周计划";
            }
            string _Lengthstart = this.GvUserPlanList.Rows[i].Cells[2].Text;
            string _Lengthend = this.GvUserPlanList.Rows[i].Cells[3].Text;
            if (_Lengthstart.Length > 9)
            {
                _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.GvUserPlanList.Rows[i].Cells[2].Text = _Lengthstart;
            }
            if (_Lengthend.Length > 9)
            {
                _Lengthend = _Lengthend.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.GvUserPlanList.Rows[i].Cells[3].Text = _Lengthend;
            }
        }
        #endregion

    }



    /// <summary>
    /// 绑定显示草稿的GridVIew事件
    /// </summary>
    void  bindCaoGaoGridView()
    {
        ListItem _type = this.drpType.SelectedItem;
        ListItem _state = this.drpState.SelectedItem;
        DataTable _UserPlan = new DataTable();
        if (_type != null && _state != null)
        {
            //月草稿
            if (_state.Value == "2" && _type.Value == "0")
            {
                //删除按钮隐藏事件
                this.GvUserPlanList.Columns[6].Visible = true;
                object[] _value = new object[3];
                _value[0] = _type.Value;
                _value[1] = 0;
                _value[2] = 0;
                //读取IsConsensus=0的所有信息
                _UserPlan = dllUserPlanMonth.getPlanSendStateFalse(_value);
                GvUserPlanList.DataSource = _UserPlan;
                GvUserPlanList.DataBind();
            }
            //周草稿
            if (_state.Value == "2" && _type.Value == "1")
            {
                //删除按钮隐藏事件
                this.GvUserPlanList.Columns[6].Visible = true;
                object[] _value = new object[3];
                _value[0] = _type.Value;
                _value[1] = 0;
                _value[2] = 0;
                //读取IsConsensus=0的所有信息
                _UserPlan = dllUserPlanMonth.getPlanSendStateFalse(_value);
                GvUserPlanList.DataSource = _UserPlan;
                GvUserPlanList.DataBind();
            }
        }

        #region 处理显示
        for (int i = 0; i < this.GvUserPlanList.Rows.Count; i++)
        {
            if (_UserPlan.Rows[i]["PlanType"].ToString() == "0")
            {
                this.GvUserPlanList.Rows[i].Cells[1].Text = "月计划";
            }
            else
            {
                this.GvUserPlanList.Rows[i].Cells[1].Text = "周计划";
            }
            string _Lengthstart = this.GvUserPlanList.Rows[i].Cells[2].Text;
            string _Lengthend = this.GvUserPlanList.Rows[i].Cells[3].Text;
            if (_Lengthstart.Length > 9)
            {
                _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.GvUserPlanList.Rows[i].Cells[2].Text = _Lengthstart;
            }
            if (_Lengthend.Length > 9)
            {
                _Lengthend = _Lengthend.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.GvUserPlanList.Rows[i].Cells[3].Text = _Lengthend;
            }
        }
        #endregion
    }



    /// <summary>
    /// 绑定显示通过的GridVIew事件
    /// </summary>
    void bindTongguoGridView()
    {
        ListItem _type = this.drpType.SelectedItem;
        ListItem _state = this.drpState.SelectedItem;
        DataTable _UserPlan = new DataTable();
        if (_type != null && _state != null)
        {
            //月审核
            if (_state.Value == "0" && _type.Value == "0")
            {
                //删除按钮隐藏事件
                this.GvUserPlanList.Columns[6].Visible = false;
                object[] _value = new object[3];
                _value[0] = _type.Value;
                _value[1] = 1;
                _value[2] = 1;
                //读取IsConsensus=0的所有信息
                _UserPlan = dllUserPlanMonth.get(UserPlanMonth.field.IsConsensus.ToString(), _value);

                GvUserPlanList.DataSource = _UserPlan;
                GvUserPlanList.DataBind();
            }
            //周审核
            if (_state.Value == "0" && _type.Value == "1")
            {
                //删除按钮隐藏事件
                this.GvUserPlanList.Columns[6].Visible = false;
                object[] _value = new object[3];
                _value[0] = _type.Value;
                _value[1] = 1;
                _value[2] = 1;
                //读取IsConsensus=0的所有信息
                _UserPlan = dllUserPlanMonth.get(UserPlanMonth.field.IsConsensus.ToString(), _value);

                GvUserPlanList.DataSource = _UserPlan;
                GvUserPlanList.DataBind();
            }
        }       

        #region 处理显示
        for (int i = 0; i < this.GvUserPlanList.Rows.Count; i++)
        {
            if (_UserPlan.Rows[i]["PlanType"].ToString() == "0")
            {
                this.GvUserPlanList.Rows[i].Cells[1].Text = "月计划";
            }
            else
            {
                this.GvUserPlanList.Rows[i].Cells[1].Text = "周计划";
            }
            string _Lengthstart = this.GvUserPlanList.Rows[i].Cells[2].Text;
            string _Lengthend = this.GvUserPlanList.Rows[i].Cells[3].Text;
            if (_Lengthstart.Length > 9)
            {
                _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.GvUserPlanList.Rows[i].Cells[2].Text = _Lengthstart;
            }
            if (_Lengthend.Length > 9)
            {
                _Lengthend = _Lengthend.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.GvUserPlanList.Rows[i].Cells[3].Text = _Lengthend;
            }
        }
        #endregion
    }



    /// <summary>
    /// 提交审核按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        AddUserPlan(1);
    }

    /// <summary>
    /// 添加员工计划的方法
    /// </summary>
    /// <param name="state">0 草稿 1 提交</param>
    void AddUserPlan(int state)
    {
        //判断结束时间是否大于开始时间
        DateTime _startTime = Convert.ToDateTime(this.txtStartTime.Text.Trim());
        DateTime _endTime = Convert.ToDateTime(this.txtEndTime.Text.Trim());
        dllAuthentication.IsEndTimeGreaterThanStartTime(_startTime,_endTime);
        //参数
        object[]  _value=new object[8];
        //获得session中用户登陆的用户名
        Model.UserInfo objUserInfo = new Model.UserInfo();
       
        _value[0] = objUserInfo.UserName;
        _value[1] = drpPlanType.Text;
        _value[2] = Convert.ToDateTime(txtStartTime.Text);
        _value[3] = Convert.ToDateTime(txtEndTime.Text);
        _value[4] = txtPlanContents.Text;
        //得到参数UserName
        object[] _valueUserInfo=new object[1];
        _valueUserInfo[0] = objUserInfo.UserName;//得到用户登陆的用户名
        //调用get方法，引用查询审核人的存储过程
        DataTable _verifyPeople= dllUserInfo.get(UserInfoManager.field.IsDepartmentManager.ToString(), _valueUserInfo);
        if (_verifyPeople.Rows.Count > 0)
        {
            _value[5] = _verifyPeople.Rows[0]["RealName"].ToString();//审核人
        }
        else
        {
            Utility.Error.RedirectErrorPage("添加失败，由于该部门无部门经理");
        }
        _value[6] = state;//PlanSendState字段，状态
        _value[7] = 0;//IsConsensus字段
        int result=dllUserPlanMonth.add(_value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("添加未成功,请重试");

        }
        else
        {
            Response.Redirect("UserPlanManager.aspx");
        }
        bindTiJiaoGridView();

        //将文本框清空
        txtStartTime.Text = string.Empty;
        txtEndTime.Text = string.Empty;
        txtPlanContents.Text = string.Empty;

    }
    /// <summary>
    /// 保存为草稿按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        AddUserPlan(0);
    }
    /// <summary>
    /// gridview的分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GvUserPlanList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GvUserPlanList.PageIndex = e.NewPageIndex;
        ListItem _type = this.drpType.SelectedItem;
        ListItem _state = this.drpState.SelectedItem;
        if (_type != null && _state != null)
        {
            if (_type.Value == "0")
            {
                if (_state.Value == "0")
                {
                    bindTongguoGridView();
                }
                if (_state.Value == "1")
                {
                    bindTiJiaoGridView();
                }
                if (_state.Value == "2")
                {
                    bindCaoGaoGridView();
                }
            }
            if (_type.Value == "1")
            {
                if (_state.Value == "0")
                {
                    bindTongguoGridView();
                }
                if (_state.Value == "1")
                {
                    bindTiJiaoGridView();
                }
                if (_state.Value == "2")
                {
                    bindCaoGaoGridView();
                }
            }
        }
    }


    

    
    /// <summary>
    /// 删除按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btn = (Button)sender;//得到当前控件
        TableCell cell = (TableCell)btn.Parent;//得到所处的父单元格
        GridViewRow Gvrow = (GridViewRow)cell.Parent;//得到所处父单元格的行
        i = Gvrow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.GvUserPlanList.Rows[i].Cells[0].Text);
        //获得该表中的ID
        int _result = dllUserPlanMonth.del(UserPlanMonth.field.MonthPlanId.ToString(), _value);
        if (_result < 0)
        {
            Utility.Error.RedirectErrorPage("删除信息不成功，请重新删除");
        }
        else
        {
            Response.Redirect("UserPlanManager.aspx");
        }
    }

    /// <summary>
    /// 计划类型改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _type = this.drpType.SelectedItem;
        ListItem _state = this.drpState.SelectedItem;
        if (_type != null && _state != null)
        {
            if (_type.Value == "0")
            {
                if (_state.Value == "0")
                {
                    bindTongguoGridView();
                }
                if (_state.Value == "1")
                {
                    bindTiJiaoGridView();
                }
                if (_state.Value == "2")
                {
                    bindCaoGaoGridView();
                }
            }
            if (_type.Value == "1")
            {
                if (_state.Value == "0")
                {
                    bindTongguoGridView();
                }
                if (_state.Value == "1")
                {
                    bindTiJiaoGridView();
                }
                if (_state.Value == "2")
                {
                    bindCaoGaoGridView();
                }
            }
        }
    }


    /// <summary>
    /// 计划状态事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpState_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _state = this.drpState.SelectedItem;
        if (_state != null)
        {
            if (_state.Value == "0")
            {
                bindTongguoGridView();
            }
            if (_state.Value == "1")
            {
                bindTiJiaoGridView();
            }
            if (_state.Value == "2")
            {
                bindCaoGaoGridView();
            }
        }        
    }
}
